<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.filter.title" placeholder="请输入标题" style="width: 200px;" col="6"></el-input>
      <el-button type="primary" @click="searchData">查询</el-button>
      <el-button type="primary" @click="openDialog(null)">新增</el-button>
    </div>

    <el-table :data="list" border style="width: 100%" v-loading="listLoading" fit highlight-current-row>
      <el-table-column fixed prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="title" label=" 标题" min-width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="destroy(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page="page" :limit="listQuery.limit" :pageSizes="pageSizes"
                @pagination="pageChange"></pagination>

    <el-dialog :title="dialogTitle" :visible.sync="formDialogIsVisible">
      <el-form :model="formData" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="formData.title" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formDialogIsVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {fetchList, add, update, destroy} from '@/api/houseType'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination

  export default {
    name: 'house_type',
    components: {Pagination},
    data() {
      return {
        list: null,
        total: 0,
        page: 1,
        pageSizes: [
          10,20,30
        ],
        listLoading: true,
        listQuery: {
          offset: 0,
          limit: 10,
          filter: {
            title: null
          },
          options: {
            title: "like"
          }
        },
        formDialogIsVisible: false,
        formMode: "add",
        formData: {
          id: undefined,
          title: undefined
        }
      }
    },
    computed: {
      dialogTitle() {
        return this.formMode === "add" ? "新增" : "编辑"
      },
    },
    created() {
      this.getList()
    },
    methods: {
      pageChange(data) {
        this.page = data.page;
        this.listQuery.limit = data.limit;
        this.listQuery.offset = (this.page - 1) * this.listQuery.limit;
        this.refresh(false);
      },
      openDialog(row) {
        if (!!row) {
          this.formMode = "edit";
          this.formData.id = row.id;
          this.formData.title = row.title;
        } else {
          this.formMode = "add";
          this.formData.id = undefined;
          this.formData.id = this.formData.title = undefined;
        }
        this.formDialogIsVisible = true;
      },
      save() {
        let promise;
        this.listLoading = true;
        if (this.formMode === "add") {
          promise = add(this.formData);
        } else {
          promise = update(this.formData);
        }
        promise.then(() => {
          this.refresh();
        }).catch(() => {
          this.listLoading = false;
        });
      },
      destroy(id) {
        this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          this.listLoading = true;
          await destroy({id});
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch((error) => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        }).finally(() => {
          this.refresh();
        });
      },
      searchData() {
        this.refresh(true);
      },
      refresh(isAll = false) {
        this.formDialogIsVisible = false;
        this.listLoading = false;
        if (isAll) {
          this.page = 1;
        }
        this.getList();
      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          let {data} = response;
          this.list = data.list
          this.total = response.data.count
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        });
      },
    },
    watch: {
      page: function (newVluae, oldVluae) {
        this.listQuery.offset = (newVluae - 1) * this.listQuery.limit;
      },
    },
  }
</script>
